package com.doit.day04

import scala.collection.mutable.{ArrayBuffer, ListBuffer}
import scala.io.Source

//1.求每部电影的平均分，按照降序输出
//电影名称，平均分==> ratings 电影id和平均分   先求电影id的平均分
object _07_需求1 {
  def main(args: Array[String]): Unit = {
    val ratings: List[String] = Source.fromFile("C:\\Users\\yi\\Desktop\\课程资料\\资料\\ratings.txt").getLines().toList
    val movies: List[String] = Source.fromFile("C:\\Users\\yi\\Desktop\\课程资料\\资料\\movies.txt").getLines().toList
    val users: List[String] = Source.fromFile("C:\\Users\\yi\\Desktop\\课程资料\\资料\\users.txt").getLines().toList
/*
    //将每行的数据进行切割
    val movie_id_avg: Map[String, Double] = ratings.map(line => {
      val arr: Array[String] = line.split("_")
      //1_1193_5_978300760
      (arr(1), arr(0), arr(2).toDouble, arr(3))
    })
      .groupBy(_._1) //Map[电影id，List[(1,2,3,4)]]
      .map(tp => {
        (tp._1, (tp._2.map(_._3).sum / tp._2.size).formatted("%.2f").toDouble)
      })
    //将movie_id 转变成 movie_name

    val map: Map[String, String] = movies.map(line => {
      val arr: Array[String] = line.split("_")
      (arr(0), arr(1))
    }).toMap

    movie_id_avg.map(tp=>{
      //因为上面是一个Map类型，可以直接get到对应的value值
      (map.getOrElse(tp._1,"没有找到"),tp._2)
    })
      .toList
      .sortBy(-_._2)
      .take(15)
      .foreach(println)*/

/*    ratings.map(line => {
      val arr: Array[String] = line.split("_")
      //1_1193_5_978300760
      (arr(1), arr(0), arr(2).toDouble, arr(3))
    }).groupBy(_._2)
      .map(tp=>{
        (tp._1,tp._2.map(_._1).distinct.size)
      })
      .toList
      .sortBy(-_._2)
      .take(15)
      .foreach(println)*/

   /* val movie_id_And_Type: Map[String, (String, String)] = movies.map(line => {
      val arr: Array[String] = line.split("_")
      (arr(0), (arr(1), arr(2)))
    }).toMap

    //3.每个用户评价的前三部电影(用户，电影信息，评分值)
    ratings.map(line => {
      val arr: Array[String] = line.split("_")
      (arr(0), arr(1), arr(2), arr(3))
    })
      .groupBy(_._1)
      .map(tp => {
        val list: List[(String, String, String, String)] = tp._2.sortBy(_._4).take(3)
        val res: List[(String, String, String, String)] = list.map(tp => {
          val movie_id: String = tp._2
          val rating: String = tp._3
          val tuple: (String, String) = movie_id_And_Type.getOrElse(movie_id, ("找不着", "找不着"))
          (movie_id, tuple._1, tuple._2, rating)
        })
        (tp._1,res)
      })
      .toList
      .sortBy(_._1)
      .take(5)
      .foreach(println)*/


    // 4.哪些年份的电影评分(平均分)最高，取最高的前五个年份
    //每个电影的年份搞出来
    //求每个电影的平均分  必须得将每一个电影id得年份搞出来 然后对应的将ratings中得电影id都换成年份
    val yearAndId: Map[String, String] = movies.map(line => {
      val arr: Array[String] = line.split("_")
      val year: String = arr(1).substring(arr(1).length - 5, arr(1).length - 1)
      (arr(0), year)
    }).toMap


    //将评分里面得电影id全部换成年份
    ratings.map(line=>{
      val arr: Array[String] = line.split("_")
      val movie_id: String = arr(1)
      val rating: String = arr(2)
      val year: String = yearAndId.getOrElse(movie_id, "9999")
      (year,rating.toDouble)
    })
      .groupBy(_._1)
      .map(tp=>{
        (tp._1,(tp._2.map(_._2).sum/tp._2.size).formatted("%.2f").toDouble)
      })
      .toList
      .sortBy(-_._2)
      .take(5)
      .foreach(println)











  }
}
